package com.chinaztt.fda.test.greenDao;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.chinaztt.fda.ui.R;
import com.chinaztt.fda.utils.ToastUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description GreenDao使用样例，参考地址  http://blog.csdn.net/u012702547/article/details/52226163
 * @Creator wushan
 * @Date 2016/8/25 14:37
 */
public class GreenDaoActivity extends ListActivity {

    private EditText editText;
    /**
     * UserText表对象
     */
    private UserTestDao  userDao;
    private SimpleAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_greendao);
        editText = (EditText) findViewById(R.id.editTextNote);
        initDB();
        refreshlist(null);
    }

    private void initDB(){
        //此处数据库初始化应该在application里
        DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(getApplicationContext(), "lenve.db", null);
        DaoMaster daoMaster = new DaoMaster(devOpenHelper.getWritableDb());
        DaoSession daoSession = daoMaster.newSession();

        //userDao UserText表对象
        userDao = daoSession.getUserTestDao();
    }

    /**
     * Button 点击的监听事件
     *
     * @param view
     */
    public void onMyButtonClick(View view) {
        switch (view.getId()) {
            case R.id.buttonAdd:
                addNote();
                break;
            case R.id.buttonQuery:
                search();
                break;
            default:
                ToastUtil.toast("What's wrong ?");
                break;
        }
    }

    private void addNote() {
        String noteText = editText.getText().toString();
        editText.setText("");

        if (noteText == null || noteText.equals("")) {
            ToastUtil.toast("Please enter a note to add");
        } else {
            // 插入操作，简单到只要你创建一个 Java 对象
        userDao.insert(new UserTest(null,"zhangsan" +noteText,"张三"));
        }

        refreshlist(null);
    }

    private void search() {
        String noteText = editText.getText().toString();
        editText.setText("");
        if (noteText == null || noteText.equals("")) {
            ToastUtil.toast("Please enter a note to query");
            refreshlist(null);
        } else {

            List<UserTest> list = userDao.queryBuilder()
                    .where(UserTestDao.Properties.Id.eq(noteText)).build().list();
            refreshlist(list);
            ToastUtil.toast("There have " + list.size() + " records");
        }
    }

    /**
     * ListView 的监听事件，用于删除一个 Item
     *
     * @param l
     * @param v
     * @param position
     * @param id
     */
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {

        //根据id删除
        userDao.deleteByKey(id);

//        //先查找，
//        UserTest user = userDao.queryBuilder().where(UserTestDao.Properties.Id.eq(id)).build().unique();
//        //再删除
//        userDao.delete(user);
        refreshlist(null);
        ToastUtil.toast("Deleted note, ID: " + id);
    }


    private void refreshlist( List<UserTest> list){

        if (list==null){
            list = userDao.queryBuilder().list();
        }

        if (list==null){
            return;
        }

        List<Map<String, Object>> listems = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < list.size(); i++) {
            UserTest user = list.get(i);
            Map<String, Object> listem = new HashMap<String, Object>();
            listem.put("nickname",user.getNickname()+"   "+user.getId());
            listem.put("username", user.getUsername());
            listems.add(listem);
        }

        String[] from = {"nickname","username"};
        int[] to = {android.R.id.text1, android.R.id.text2};

        adapter = new SimpleAdapter(this,listems, android.R.layout.simple_list_item_2,from,to);
        setListAdapter(adapter);
    }
}
